package com.itheima.config;

import com.itheima.common.utils.MD5Utils;
import com.itheima.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
public class SecurityConfig {
    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
                // 配置授权规则
                .authorizeRequests(auth -> auth
                        .antMatchers(
                                "/mobile/pages/login.html",
                                "/mobile/pages/index.html",
                                "/mobile/login.html",
                                "/validatecode/**",
                                "/member/login",
                                "/mobile/css/**",
                                "/mobile/js/**",
                                "/mobile/img/**",
                                "/mobile/fonts/**",
                                "/mobile/plugins/**",
                                "/setmeal/**",
                                "/order/**",
                                "/mobile/pages/**"
                        ).permitAll()
                        .anyRequest().authenticated()
                )
                // ★ 禁用表单登录 ★
                .formLogin().disable()
                // 安全配置
                .csrf().disable()
                .headers().frameOptions().sameOrigin()
                .and()
                // 无状态会话
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // 添加JWT过滤器
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        return http.build();
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return new PasswordEncoder() {
            @Override
            public String encode(CharSequence rawPassword) {
                return MD5Utils.md5(rawPassword.toString());
            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                return encodedPassword.equals(MD5Utils.md5(rawPassword.toString()));
            }
        };
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception {
        return config.getAuthenticationManager();
    }
}
